package app.project.streetlensproject.map;

import java.io.IOException;
import java.util.ArrayList;

import com.google.android.gms.maps.model.LatLng;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import app.project.streetlensproject.HomeActivity;
import app.project.streetlensproject.R;
import app.project.streetlensproject.adapter.ItemListPlaceDirectionAdapter;
import app.project.streetlensproject.model.Place;
import app.project.streetlensproject.util.Constants;
import app.project.streetlensproject.util.Utilities;

public class PlaceDirectionDetailActivity extends Activity implements
		OnClickListener {

	private Button mButtonDriving, mButtonCycling, mButtonWalking;
	private ListView mListViewInstruction;
	public static final LatLng HANOI = new LatLng(20.993776, 105.811417);
	public LatLng MYPLACE;
	public Place place;
	
	@Override
	public void onCreate(Bundle circle) {
		super.onCreate(circle);
		setContentView(R.layout.activity_place_direction_detail);

		mButtonDriving = (Button) findViewById(R.id.buttonDrivingDetail);
		mButtonCycling = (Button) findViewById(R.id.buttonCyclingDetail);
		mButtonWalking = (Button) findViewById(R.id.buttonWalkingDetail);
		mListViewInstruction = (ListView) findViewById(R.id.listViewPlaceInstruction);

		MYPLACE=new LatLng(HomeActivity.MY_LATITUDE, HomeActivity.MY_LONGITUDE);
		
		String typeDirection = getIntent().getStringExtra("typeDirection");
		place = (Place) getIntent().getSerializableExtra("place");
		
		mButtonDriving.setOnClickListener(this);
		mButtonCycling.setOnClickListener(this);
		mButtonWalking.setOnClickListener(this);

		String urlPlaces = Utilities.getInstance().makeURL(MYPLACE.latitude,
				MYPLACE.longitude, place.locationx, place.locationy,
				typeDirection);

		Log.e("URL", urlPlaces);
		new getPathPlaceTask(urlPlaces).execute();

	}

	/**
	 * Task get place from DB
	 * 
	 * @author tuanshaker
	 * 
	 */
	public class getPathPlaceTask extends AsyncTask<Void, Void, String> {
		private ProgressDialog progressDialog;
		String url;

		getPathPlaceTask(String urlPass) {
			url = urlPass;
		}

		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			super.onPreExecute();
			progressDialog = new ProgressDialog(
					PlaceDirectionDetailActivity.this);
			progressDialog.setMessage(getResources().getString(
					R.string.name_loading_map));
			progressDialog.setIndeterminate(true);
			progressDialog.show();
		}

		@Override
		protected String doInBackground(Void... params) {
			// JSONParser jParser = new JSONParser();
			String json = "";
			try {
				json = Utilities.getInstance().readJsonFromUrl(url);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return json;
		}

		@Override
		protected void onPostExecute(String result) {
			super.onPostExecute(result);
			progressDialog.hide();
			if (result != null) {
				ArrayList<String> arrListInstruction = Utilities.getInstance()
						.getHtmlInstruction(result);
				mListViewInstruction
						.setAdapter(new ItemListPlaceDirectionAdapter(
								PlaceDirectionDetailActivity.this,
								arrListInstruction));
			}
		}
	}

	public void onClick(View view) {
		// TODO Auto-generated method stub
		switch (view.getId()) {
		case R.id.buttonDrivingDetail:
			String urlPlaces = Utilities.getInstance().makeURL(MYPLACE.latitude,
					MYPLACE.longitude, place.locationx, place.locationy,
					Constants.MODE_DRIVING);
			new getPathPlaceTask(urlPlaces).execute();
			break;
		case R.id.buttonCyclingDetail:
			String urlPlaces1 = Utilities.getInstance().makeURL(MYPLACE.latitude,
					MYPLACE.longitude, place.locationx, place.locationy,
					Constants.MODE_CYCLING);
			new getPathPlaceTask(urlPlaces1).execute();
			break;
		case R.id.buttonWalkingDetail:
			String urlPlaces2 = Utilities.getInstance().makeURL(MYPLACE.latitude,
					MYPLACE.longitude, place.locationx, place.locationy,
					Constants.MODE_WALKING);
			new getPathPlaceTask(urlPlaces2).execute();
			break;

		default:
			break;
		}
	}
}
